package com.xiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.domain.ResponseResult;
import com.xiao.domain.dto.TagListDto;
import com.xiao.domain.vo.BlogTagVo;
import com.xiao.enums.AppHttpCodeEnum;
import com.xiao.mapper.BlogTagMapper;
import com.xiao.domain.pojo.BlogTag;
import com.xiao.domain.vo.PageVo;
import com.xiao.service.BlogTagService;
import com.xiao.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

// 标签表 业务实现

@Service
public class BlogTagServiceImpl extends ServiceImpl<BlogTagMapper, BlogTag> implements BlogTagService {
    @Override
    public ResponseResult deleteById(List<Integer> ids) {

        Integer i = this.getBaseMapper().deleteBatchIds(ids);
        ResponseResult responseResult ;
        if (i.equals(ids.size())){
            responseResult = ResponseResult.okResult();
        }else {
            responseResult = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }

        return responseResult;
    }

    @Override
    public ResponseResult<BlogTagVo> queryById(Integer id) {

        BlogTag blogTag = this.getBaseMapper().selectById(id);

        BlogTagVo blogTagVo = BeanCopyUtils.copyBean(blogTag, BlogTagVo.class);

        ResponseResult<BlogTagVo> result = ResponseResult.okResult(blogTagVo);

        return result;
    }

    @Override
    public ResponseResult saveUpdate(BlogTagVo blogTagVo) {
        BlogTag blogTag = BeanCopyUtils.copyBean(blogTagVo, BlogTag.class);
        boolean isSave = this.updateById(blogTag);

        ResponseResult responseResult;
        if(isSave){
            responseResult = ResponseResult.okResult();
        }else {
            responseResult = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        return responseResult;
    }

    @Override
    public ResponseResult addTag(BlogTagVo blogTagVo) {
        ResponseResult responseResult;

        BlogTag blogTag = BeanCopyUtils.copyBean(blogTagVo, BlogTag.class);

        boolean save = this.save(blogTag);
        if (save){
            responseResult=ResponseResult.okResult();
        }else {
            responseResult=ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }

        return responseResult;
    }

    @Override
    public ResponseResult<BlogTagVo> listAllTag() {

        List<BlogTag> list = this.list();
        ResponseResult responseResult = ResponseResult.okResult(list);

        return responseResult;
    }

    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        // 分页查询
        LambdaQueryWrapper<BlogTag> wrapper = new LambdaQueryWrapper<>();
        // 如果该字段不为空就查询
        wrapper.like(StringUtils.hasText(tagListDto.getName()),BlogTag::getName,tagListDto.getName());
        wrapper.like(StringUtils.hasText(tagListDto.getRemark()),BlogTag::getRemark,tagListDto.getRemark());

        Page<BlogTag> page = new Page<>();
        page.setCurrent(pageNum);// 第几页
        page.setSize(pageSize);// 每页展示多少数据
        this.page(page,wrapper);
        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());
        // 统一响应
        ResponseResult responseResult = ResponseResult.okResult(pageVo);

        return responseResult;
    }

}